package com.example.ss.controller;

import com.example.ss.entity.Material;
import com.example.ss.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author: xwg
 * @CreateDate: 2023/8/25
 */

@Controller
//  传统mvc  前后端分离 json
public class MaterialController {

    @GetMapping("/current")
    @ResponseBody
    public String getCurrentTime() {
//        return  System.currentTimeMillis();
        return LocalDateTime.now().toString();
    }

    @GetMapping("/currentUser")
    @ResponseBody
    public String getCurrentUser() {
//        return  System.currentTimeMillis();
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("后端通过cookie获取username"+name);
        return name ;
    }
//    什么是身份认证
//    指的是 后端的每一个controller 都有访问人信息
    @Autowired
    private MaterialService materialService;

    //    新增material 接口
    @PostMapping("/insert")
    @ResponseBody
    public Material insert(@RequestBody Material material) {
        System.out.println(material);
        material.setMaterialCode("MA00111"+ ThreadLocalRandom.current().nextInt(10000));
        materialService.insert(material);
        return material;
    }
//    swagger
//    身份认证  login 本质
//    使用明文的用户名+密码 换一个 暗文的用户名 token / cookie
//    权限拦截
}
